﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using YBApproval.IDAL;
using YBApproval.Model;
using System.Data.Entity;

namespace YBApproval.EFDAL
{
    public class ApprovalDAL: IApproval
    {
        private YBDbContext db = new YBDbContext();

        public List<ApprovalView> GetApprovals(int processId, int nodeOrder)
        {
            var approval = from p in db.Approvals
                           where (from n in db.Nodes where n.ProcessId == processId && n.Order == nodeOrder select n.Id).Contains(p.NodeId)
                           select p;                           
            List<Approval> list = approval.ToList();
            List<ApprovalView> viewList = new List<ApprovalView>();
            foreach (var item in list)
            {
                ApprovalView viewModel = new ApprovalView()
                {
                    Id = item.Id,
                    ApproverId = item.ApproverId.ToString(),
                    IsRole = item.IsRole,
                    NodeId = item.NodeId
                };
                if (item.IsRole)
                {
                    viewModel.ApproverName = db.Roles.Find(item.ApproverId).Name;
                }
                else
                {
                    viewModel.ApproverName = db.UserProfiles.Find(item.ApproverId).Name;
                }
                viewList.Add(viewModel);
            }
            return viewList;
        }

        public bool Add(Approval approval)
        {
            throw new NotImplementedException();
        }

        public bool Update(Approval approval)
        {
            throw new NotImplementedException();
        }

        public bool Delete(Approval approval)
        {
            throw new NotImplementedException();
        }

        public bool InsertOrUpdate(Approval approval)
        {
            
            throw new NotImplementedException();
        }

        public void Dispose()
        {
            db.Dispose();
        }
    }
}
